package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.utils.JwtUtils;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private  UserMapper userMapper;


    @Override
    public PageResult<User> list(UserDto userDto) {
        //1.设置分页参数
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());
        //2.调用Mapper查询数据
        List<User> userList = userMapper.selectList(userDto);

        //3.解析返回封装结果
        Page<User> page = (Page<User>) userList;
        return new PageResult<>(page.getTotal(),page.getResult());
    }

    @Override
    public void addUser(User user) {
        //1.补全属性
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        //2.设置默认密码
        String password =user.getUsername() + "123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userMapper.insertUser(user);

    }

    @Override
    public void deleteUserById(List<Integer> ids) {
        userMapper.deleteUserById(ids);
    }

    @Override
    public void updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(user);

    }

    @Override
    public User selectUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> selectAllUser() {
        userMapper.usersAll();
        return userMapper.usersAll();
    }

    @Override
    public List<User> selectUserByRole(String roleLabel) {
        userMapper.selectUserByRole(roleLabel);
        return userMapper.selectUserByRole(roleLabel);
    }

    @Override
    public LoginResultVo login(User user) {
        User u = userMapper.login(user.getUsername());
        if (u == null){
            log.info("用户不存在");
            throw new BusinessException("用户名或密码错误");
        }
        String _pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if (!_pass.equals(u.getPassword())){
            log.info("密码不正确");
            throw new BusinessException("用户名或密码错误");
        }
        //4.判断状态是否正确
        if (u.getStatus() != 1){
            log.info("用户已停用");
            throw new BusinessException("用户已停用");
        }
        //5.封装结果并保存
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(u.getId());
        loginResultVo.setUsername(u.getUsername());
        loginResultVo.setName(u.getName());
        loginResultVo.setImage(u.getImage());
        loginResultVo.setRoleLabel(u.getRoleLabel());
        loginResultVo.setToken("");
        //调用工具类，生成jwt令牌
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",u.getId());
        claims.put("username",u.getUsername());
        String jwt = JwtUtils.generateToken(claims);
        loginResultVo.setToken(jwt);

        return loginResultVo;
    }
}
